﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Project.Repository;
using Project.IManager;
using NHibernate.Criterion;
using System.Collections;
using Wuqi.Webdiyer;

namespace Project.Manager
{
    public abstract class AbstractManager<T> : IManagerBase<T> where T : class
    {
        public IRepository<T> Dao { get; set; }

        public virtual T Get(object id)
        {
            return this.Dao.Get(id);
        }

        public virtual object Save(T model)
        {
            // 摘抄后注释的代码
            //IDomainEntity entity = model as IDomainEntity;
            //if (entity != null)
            //{
            //    entity.CreateTime = DateTime.Now;
            //    entity.EditTime = DateTime.Now;
            //    entity.CreateUser = "LiuDong";
            //    entity.EditUser = "LiuDong";
            //}
            return this.Dao.Save(model);
        }

        public void Update(T model)
        {
            // 摘抄后注释的代码
            //IDomainEntity entity = model as IDomainEntity;
            //if (entity != null)
            //{
            //    entity.EditTime = DateTime.Now;
            //    entity.EditUser = "LiuDong";
            //}
            this.Dao.Update(model);
        }

        public void SaveOrUpdate(T model)
        {
            // 摘抄后注释的代码
            //IDomainEntity entity = model as IDomainEntity;
            //if (entity != null)
            //{
            //    if (entity.CreateTime <= DateTime.Parse("1900-01-01"))
            //    {
            //        entity.CreateTime = DateTime.Now;
            //    }
            //    entity.EditTime = DateTime.Now;
            //    if (string.IsNullOrEmpty(entity.CreateUser))
            //    {
            //        entity.CreateUser = "LiuDong";
            //    }
            //    entity.EditUser = "LiuDong";
            //}
            this.Dao.SaveOrUpdate(model);
        }

        public virtual void Delete(T model)
        {
            this.Dao.Delete(model);
        }

        public virtual void Delete(object id)
        {
            this.Dao.Delete(id);
        }

        public void Delete(List<long> idList)
        {
            foreach (long item in idList)
            {
                T model = this.Dao.Get(item);
                if (model != null)
                {
                    this.Dao.Delete(model);
                }
            }
        }

        public virtual int GetCount()
        {
            return this.Dao.GetCount();
        }
        public virtual int GetCount(IList<ICriterion> list)
        {
            return this.Dao.GetCount(list);
        }

        public virtual IQueryable<T> GetAll()
        {
            return this.Dao.GetAll();
        }

		public virtual IList<T> GetList(IList<ICriterion> list)
        {
			return this.Dao.GetByCriteria(list);
        }
		public virtual IList<T> GetList(IList<ICriterion> list,IList<Order> orderList)
        {
            return this.Dao.GetByCriteria(list, orderList);
        }
        public virtual IList GetList(string queryString)
        {
            return this.Dao.GetList(queryString);
        }
        public virtual IList<TT> GetDistinctList<TT>(string columnName)
        {
            return this.Dao.GetDistinctList<TT>(columnName);
        }

        public virtual IQueryable<T> GetPageList(int firstResult, int maxResult)
        {
            return this.Dao.GetPageList(firstResult, maxResult);
        }

        public virtual IQueryable<T> FindPageList(System.Reflection.PropertyInfo sort, string dir, int firstResult, int maxResult)
        {
            return this.Dao.FindPageList(sort, dir, firstResult, maxResult);
        }

        public virtual IQueryable<T> FindPageList(out long total, System.Reflection.PropertyInfo sort, string dir, int firstResult, int maxResult)
        {
            return this.Dao.FindPageList(out total, sort, dir, firstResult, maxResult);
        }

        // 动态分页查询
        public IList<T> GetPageList(int firstResult, int maxResult, IList<NHibernate.Criterion.ICriterion> list, NHibernate.Criterion.Order order)
        {
            return this.Dao.GetPageList(firstResult, maxResult, list, order);
        }
        public IList<T> GetPageList(AspNetPager AspNetPager, IList<ICriterion> queryList, Order order)
        {
            //// 总记录数在 IsPostBack = false (页面第一次加载)的时候赋值,切换分页的时候不需要再次查询
            ////AspNetPager.RecordCount = this.GetCount(queryList);

            //int firstResult = (AspNetPager.CurrentPageIndex - 1) * AspNetPager.PageSize;
            //int maxResult = AspNetPager.CurrentPageIndex * AspNetPager.PageSize;

            // 默认按照 Id 倒序排序
            if (order == null)
            {
                order = Order.Desc("Id");
            }

            //return this.Dao.GetPageList(firstResult, maxResult, queryList, order);
            return this.Dao.GetPageList(AspNetPager, queryList, order);
        }
        public IList<T> GetPageList(AspNetPager AspNetPager, IList<ICriterion> queryList, IList<Order> orderList)
        {
            //// 总记录数在 IsPostBack = false (页面第一次加载)的时候赋值,切换分页的时候不需要再次查询
            ////AspNetPager.RecordCount = this.GetCount(queryList);

            //int firstResult = (AspNetPager.CurrentPageIndex - 1) * AspNetPager.PageSize;
            //int maxResult = AspNetPager.CurrentPageIndex * AspNetPager.PageSize;

            // 默认按照 Id 倒序排序
            if (orderList.Count == 0 || orderList.IndexOf(Order.Desc("Id")) == -1)
            {
                orderList.Add(Order.Desc("Id"));
            }

            //return this.Dao.GetPageList(firstResult, maxResult, queryList, order);
            return this.Dao.GetPageList(AspNetPager, queryList, orderList);
        }
    }
}
